/*************************
分数汇总
* */
<template>
  <div class="boxsty zhiBiao">

      <div :height="searchheight" v-loading.fullscreen.lock="fullscreenLoading">
      <span
        style="float: right; margin-top: -30px; cursor: pointer"
        @click="searchchangeadvanced"
      >{{ searchadvanced ? "收起" : "展开" }}
        <i
          :class="searchadvanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
        />
      </span>
      <div v-show="searchadvanced">

          <el-form  :model="searchForm"  class="form-inline" >
            <el-row>
              <el-col :span="24">
                <el-col :span="6">
                  <el-form-item label="考核对象">
                    <el-input v-model="searchForm.khdx" placeholder="考核对象" :clearable="true" style="width:70%"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="7">
                  <el-form-item label="工号">
                    <el-input v-model="searchForm.ygbm" placeholder="工号" :clearable="true" style="width:60%"></el-input>
                  </el-form-item>
              </el-col>
                <el-col :span="11">
                  <el-form-item>
                    <el-button
                      icon="el-icon-search"
                      type="primary"
                      class="marginLeft"
                      @click="getDataList(1)"
                    >检索</el-button>
                  </el-form-item>
                </el-col>
              </el-col>
            </el-row>
       <!--     <el-row>
              <el-col :span="24">
                <el-form-item label="部门">
                  <el-checkbox :indeterminate="isIndeterminateBm" v-model="checkAll"
                               @change="handleCheckAllChange" class="fla">全选</el-checkbox>
                  <el-checkbox-group v-model="checkedBms" @change="handleCheckedBmsChange" class="fla">
                    <el-checkbox v-for="bms in bmsArr" :label="bms" :key="bms">{{bms}}</el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
              </el-col>
            </el-row>-->

            <el-row>
              <el-col :span="24">
              <el-form-item label="岗位">
                <el-checkbox :indeterminate="isIndeterminateGw" v-model="checkAllGw"
                             @change="handleCheckAllChangeGw" class="fla">全选</el-checkbox>
                <el-checkbox-group v-model="checkedGws" @change="handleCheckedGwsChange" class="fla">
                  <el-checkbox v-for="gws in gwsArr" :label="gws" :key="gws">{{gws}}</el-checkbox>
                </el-checkbox-group>
              </el-form-item>
              </el-col>
            </el-row>

          </el-form>
      <div style="border-bottom: 1px solid #b0bde2">

      </div>
      <div class="mod-config" style="margin-top: 20px">
        <div>
          <el-button
            type="primary"
            @click="fenshu"
            :disabled="canSubmit"
          >加载分数</el-button>

          <el-button
            type="primary"
            @click="delFormData"
            :disabled="canSubmit"
          >删除</el-button>

          <div class="pull-right">
            <el-button
              type="primary"
              style="margin-left: 10px"
              plain
              @click="saveFormData"
              :disabled="canSubmit"
            >确定</el-button
            >
            <el-button
              plain
              type="primary"
              style="margin-left: 10px"
              @click="fnBackOldPage('返回原页面')"
            >返回</el-button
            >
          </div>


        </div>

        <div >
          <el-table
            style="width: 100%; margin-top: 20px"
            :data="dataList"
            border
            v-show="tabType == 1"
            v-loading="dataListLoading"
            @selection-change="handleSelectionChange"
            ref="table2"
          >
            <el-table-column type="selection" width="55" align="center"></el-table-column>
            <el-table-column
              type="index"
              width="80"
              header-align="center"
              align="center"
              label="序号"
            ></el-table-column>
            <el-table-column
              prop="ygbm"
              header-align="center"
              align="center"
              width="120"
              label="员工编号"
              show-overflow-tooltip
            >
              <!-- <template slot-scope="scope">
                 <el-input v-model="scope.row.ygbh" placeholder="员工编号" />
               </template>-->
            </el-table-column>

            <el-table-column
              prop="ygname"
              header-align="center"
              align="center"
              width="120"
              label="考核对象"
              show-overflow-tooltip
            >
              <!--  <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.name"
                    placeholder="姓名"
                  />
                </template>-->
            </el-table-column>
            <el-table-column
              prop="deptName"
              header-align="center"
              align="center"
              width="120"
              label="部门"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              v-if="show"
              prop="deptId"
              header-align="center"
              align="center"
              label="部门id"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              prop="gwmc"
              header-align="center"
              align="center"
              width="120"
              label="岗位"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              v-if="show"
              prop="gwid"
              header-align="center"
              align="center"
              label="岗位id"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column align="center" label="测评">
              <el-table-column
                prop="cpScore"
                header-align="center"
                align="center"
                width="100"
                label="得分"
                show-overflow-tooltip
              >
              </el-table-column>
              <el-table-column
                prop="cpQz"
                header-align="center"
                align="center"
                width="100"
                label="权重"
                show-overflow-tooltip
              >
                <template slot-scope="scope">
                  <el-input
                    v-model.number="scope.row.cpQz"
                    placeholder="权重"
                    oninput="value=value.replace(/[^\d.]/g, '').replace(/\.{2,}/g, '.').replace('.', '$#$').replace(/\./g, '').replace('$#$', '.').replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3').replace(/^\./g, '')"
                    @input="addCpQz(scope.row, scope.$index)"
                  />
                </template>
              </el-table-column>
            </el-table-column>
            <el-table-column align="center" label="日常考核">
              <el-table-column
                prop="rcScore"
                header-align="center"
                align="center"
                width="100"
                label="得分"
                show-overflow-tooltip
              >
              </el-table-column>
              <el-table-column
                prop="rcQz"
                header-align="center"
                align="center"
                width="100"
                label="权重"
                show-overflow-tooltip
              >
                <template slot-scope="scope">
                  <el-input
                    v-model.number="scope.row.rcQz"
                    placeholder="权重"
                    oninput="value=value.replace(/[^\d.]/g, '').replace(/\.{2,}/g, '.').replace('.', '$#$').replace(/\./g, '').replace('$#$', '.').replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3').replace(/^\./g, '')"
                    @input="addRcQz(scope.row, scope.$index)"
                  />
                </template>
              </el-table-column>
            </el-table-column>
            <el-table-column align="center" label="业绩合同">
              <el-table-column
                prop="htScore"
                header-align="center"
                align="center"
                width="100"
                label="得分"
                show-overflow-tooltip
              >
              </el-table-column>
              <el-table-column
                prop="htQz"
                header-align="center"
                align="center"
                width="100"
                label="权重"
                show-overflow-tooltip
              >
                <template slot-scope="scope">
                  <el-input
                    v-model.number="scope.row.htQz"
                    placeholder="权重"
                    oninput="value=value.replace(/[^\d.]/g, '').replace(/\.{2,}/g, '.').replace('.', '$#$').replace(/\./g, '').replace('$#$', '.').replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3').replace(/^\./g, '')"
                    @input="addHtQz(scope.row, scope.$index)"
                  />
                </template>
              </el-table-column>
            </el-table-column>
            <el-table-column align="center" label="重点任务">
              <el-table-column
                prop="rwScore"
                header-align="center"
                align="center"
                width="100"
                label="得分"
                show-overflow-tooltip
              >
              </el-table-column>
              <el-table-column
                prop="rwQz"
                header-align="center"
                align="center"
                width="100"
                label="权重"
                show-overflow-tooltip
              >
                <template slot-scope="scope">
                  <el-input
                    v-model.number="scope.row.rwQz"
                    placeholder="权重"
                    oninput="value=value.replace(/[^\d.]/g, '').replace(/\.{2,}/g, '.').replace('.', '$#$').replace(/\./g, '').replace('$#$', '.').replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3').replace(/^\./g, '')"
                    @input="addRwQz(scope.row, scope.$index)"
                  />
                </template>
              </el-table-column>
            </el-table-column>
            <el-table-column align="center" label="工作任务书">
              <el-table-column
                prop="gzScore"
                header-align="center"
                align="center"
                width="100"
                label="得分"
                show-overflow-tooltip
              >
              </el-table-column>
              <el-table-column
                prop="gzQz"
                header-align="center"
                align="center"
                width="100"
                label="权重"
                show-overflow-tooltip
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.gzQz"
                    placeholder="权重"
                    oninput="value=value.replace(/[^\d.]/g, '').replace(/\.{2,}/g, '.').replace('.', '$#$').replace(/\./g, '').replace('$#$', '.').replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3').replace(/^\./g, '')"
                    @input="addGzQz(scope.row, scope.$index)"

                  />
                </template>
              </el-table-column>
            </el-table-column>
            <el-table-column
              prop="xs"
              header-align="center"
              align="center"
              width="120"
              label="系数"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.xs"
                  placeholder="系数"
                  oninput="value=value.replace(/[^\-\d.]/g, '').replace(/^\./g, '').replace(/\.{2,}/g, '.').replace(/(\.\d+)\./g, '$1').replace(/(-)\./g, '$1').replace(/\-{2,}/g, '-').replace(/(\d+|\.)-/g, '$1').replace(/-(0){2,}/g, '$1').replace(/(-)0+(\d+)/g, '$1$2').replace(/^0+(\d)/, '$1').replace(/(\d{15})\d*/, '$1').replace(/(\.\d{2})\d*/, '$1')"
                  @input="addXs(scope.row, scope.$index)"
                />
              </template>
            </el-table-column>
            <el-table-column
              prop="zhdf"
              header-align="center"
              align="center"
              width="120"
              fixed="right"
              label="汇总得分"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              prop="zzdf"
              header-align="center"
              align="center"
              width="120"
              fixed="right"
              label="最终得分"
              show-overflow-tooltip
            >
            </el-table-column>

          </el-table>
        </div>
      </div>


      </div>
      </div>
     <!-- <ScoreZxForm ref="eventzxRef" :scorezxForm="dataForm" :zid="zid"></ScoreZxForm>-->
<!--      子列表-->
     <!-- <span slot="footer" class="dialog-footer">
        <el-button type="primary" plain @click="closeDialog()">返回</el-button>
        <el-button  type="primary" plain @click="saveFormData">确定</el-button>
      </span>-->


  </div>
</template>

<script>
    //import ScoreZxForm from "@/views/statisticsanalysis/departSummary/score-zx-form.vue"
    // 接口 api
    import {
        getSysDeptDtoList,
        getGwmcDtoList,
        getBmhzmxByZid,
        submitBmhzMx,
        deleteScore,
        getMxListQuery
    } from "@/api/statisticsanalysis/pfhzbmhzApi.js";
    import {
        submitEjld,
    } from "@/api/statisticsanalysis/leaderApi.js";
    const bms = ['运营管理分部', '薪酬服务分部', '员工服务分部', '公司领导', '综合分部'];
    const gws = ['岗位1', '岗位2', '岗位3', '岗位4', '岗位5'];
    export default {
        components: {

        },
        props: {
            dialogTitle: String
        },
        data() {

            return {
                canSubmit: true,
                visible: false, //最外弹窗显隐
                searchadvanced: true,
                searchheight: "0",
                fullscreenLoading: false,
                searchForm:{},
                //dataForm:{},
                checkAll: false,
                checkedBms: [], // 初始选中的值
                bmsArr: [], //一共有多少个选项
                isIndeterminateBm: false, // 全选框是否在勾选状态

                checkAllGw: false,
                checkedGws: [], // 初始选中的值
                gwsArr: [], //一共有多少个选项
                isIndeterminateGw: false, // 全选框是否在勾选状态
                zid:'',
                tabType: "1",
                scorezxForm: {},
                usercomment: false, //人员选择显隐
                show:false,
                pageIndex: 1,
                pageSize: 10,
                totalPage: 0,
                type: 0,
                index: 1,
                dataList: [],
                dataListLoading: false,
                dialogVisible: false,

            };
        },
        watch: {},
        created() {

        },

        methods: {
            init(obj) {
                this.visible = true;
                this.zid = obj.id;
                if (obj.state == 0 || obj.state == 5) {
                    this.canSubmit = false;
                } else {
                    this.canSubmit = true;
                }
                this.getSysDeptDtoList();//部门集合
                this.getGwmcDtoList();//岗位集合
                this.getDataList();
            },
             // 勾选数据
            handleSelectionChange(val) {

                this.multipleSelection = val;
            },

            selectSecondSelect(){
                //选择的时候强制刷新
                this.$forceUpdate()
            },
            getSysDeptDtoList(){
                this.bmsArr = [];
                getSysDeptDtoList(this.zid).then(res => {
                    res.data.data.map(it => {
                        this.bmsArr.push(it.deptName)
                    });

                });
            },
            getGwmcDtoList(){
                this.gwsArr = [];
                getGwmcDtoList(this.zid).then(res => {
                    res.data.data.map(it => {
                        this.gwsArr.push(it.gwmc)
                    });

                });

            },
            handleCheckAllChange(val) {
                this.checkedBms = val ? bms : [];
                this.isIndeterminateBm = false;

            },
            handleCheckedBmsChange(value) {
                let checkedCount = value.length;
                this.checkAll = checkedCount === this.bmsArr.length;
                this.isIndeterminateBm = checkedCount > 0 && checkedCount < this.bmsArr.length;
            },

            handleCheckAllChangeGw(val) {
                this.checkedGws = val ? gws : [];
                this.isIndeterminateGw = false;

            },
            handleCheckedGwsChange(value) {
                let checkedCount = value.length;
                this.checkAllGw = checkedCount === this.gwsArr.length;
                this.isIndeterminateGw = checkedCount > 0 && checkedCount < this.gwsArr.length;
            },
            searchchangeadvanced() {
                this.searchadvanced = !this.searchadvanced;
                if (this.searchadvanced) {
                    this.searchheight = "auto";
                }
            },
            fenshu(){
                submitEjld(this.zid).then(res => {
                    this.getDataList();

                });


            },
            addCpQz(row, index){
                let data = [];
                data = this.$refs.table2.selection;
                if(data.length != 0){
                    let  checked = [];
                    let  inputId = row.id;
                    data.map(ele => {
                        checked.push(ele.id);
                    });

                    data.map(it => {

                        if(checked.includes(inputId)){
                            it.cpQz = row.cpQz;
                            //系数 权重 置空
                            it.xs = '';
                            it.zhdf = ''
                            it.zzdf = '';

                        }
                    });
                }
                //两数组对象，获取不同的两数组对象

                let getArrayList = this.getArrDifSameValue(this.$refs.table2.selection,this.dataList)

                getArrayList.map(it => {
                    if(it.id == row.id){
                        it.xs = '';
                        it.zhdf = ''
                        it.zzdf = '';
                    }
                })

            },
            addRcQz(row, index){
                let data = [];
                data = this.$refs.table2.selection;
                if(data.length != 0){
                    let  checked = [];
                    let  inputId = row.id;
                    data.map(ele => {
                        checked.push(ele.id);
                    });

                    data.map(it => {

                        if(checked.includes(inputId)){
                            it.rcQz = row.rcQz;
                            it.xs = '';
                            it.zhdf = ''
                            it.zzdf = '';


                        }
                    });
                }
                //两数组对象，获取不同的两数组对象

                let getArrayList = this.getArrDifSameValue(this.$refs.table2.selection,this.dataList)

                getArrayList.map(it => {
                    if(it.id == row.id){
                        it.xs = '';
                        it.zhdf = ''
                        it.zzdf = '';
                    }
                })
            },
            addHtQz(row, index){
                let data = [];
                data = this.$refs.table2.selection;
                if(data.length != 0){
                    let  checked = [];
                    let  inputId = row.id;
                    data.map(ele => {
                        checked.push(ele.id);
                    });

                    data.map(it => {

                        if(checked.includes(inputId)){
                            it.htQz = row.htQz;
                            it.xs = '';
                            it.zhdf = ''
                            it.zzdf = '';

                        }
                    });
                }
                //两数组对象，获取不同的两数组对象

                let getArrayList = this.getArrDifSameValue(this.$refs.table2.selection,this.dataList)

                getArrayList.map(it => {
                    if(it.id == row.id){
                        it.xs = '';
                        it.zhdf = ''
                        it.zzdf = '';
                    }
                })
            },
            addRwQz(row, index){
                let data = [];
                data = this.$refs.table2.selection;
                if(data.length != 0){
                    let  checked = [];
                    let  inputId = row.id;
                    data.map(ele => {
                        checked.push(ele.id);
                    });

                    data.map(it => {

                        if(checked.includes(inputId)){
                            it.rwQz = row.rwQz;
                            it.xs = '';
                            it.zhdf = ''
                            it.zzdf = '';

                        }
                    });
                }
                //两数组对象，获取不同的两数组对象

                let getArrayList = this.getArrDifSameValue(this.$refs.table2.selection,this.dataList)

                getArrayList.map(it => {
                    if(it.id == row.id){
                        it.xs = '';
                        it.zhdf = ''
                        it.zzdf = '';
                    }
                })
            },


            addGzQz(row, index){

                let data = [];
                data = this.$refs.table2.selection;
                if(data.length != 0){
                    let  checked = [];
                    let  inputId = row.id;
                    data.map(ele => {
                        checked.push(ele.id);
                    });

                    data.map(it => {

                        if(checked.includes(inputId)){
                            it.gzQz = row.gzQz;
                            it.xs = '';
                            it.zhdf = ''
                            it.zzdf = '';

                        }
                    });
                }

                //两数组对象，获取不同的两数组对象

                let getArrayList = this.getArrDifSameValue(this.$refs.table2.selection,this.dataList)

                getArrayList.map(it => {
                    if(it.id == row.id){
                        it.xs = '';
                        it.zhdf = ''
                        it.zzdf = '';
                    }
                })
            },
            addXs(row, index){
                let data = [];
                data = this.$refs.table2.selection;
                if(data.length != 0){
                    let  checked = [];
                    let  inputId = row.id;
                    data.map(ele => {
                        checked.push(ele.id);
                    });

                    data.map(it => {

                        if(checked.includes(inputId)){
                            it.xs = row.xs;

                            if(it.cpQz == null || it.rcQz == null || it.htQz == null || it.rwQz == null || it.gzQz == null){

                                this.$message.warning("所有权重不能为空");
                                throw new Error('end') // 终止循环

                            }

                            if(it.xs != null && it.xs != ""){
                                let zf = Number(((it.cpScore*it.cpQz/100)+(it.rcScore*it.rcQz/100)+(it.htScore*it.htQz/100)+(it.rwScore*it.rwQz/100)+(it.gzScore*it.gzQz/100))*it.xs);
                                it.zhdf = this.numFilter(zf);
                                it.zzdf = this.numFilter(zf);

                            }else{
                                it.zhdf = ''
                                it.zzdf = '';
                            }


                        }


                    });
                }

                //两数组对象，获取不同的两数组对象

                let getArrayList = this.getArrDifSameValue(this.$refs.table2.selection,this.dataList)

                getArrayList.map(it => {
                    if(it.id == row.id){
                        if(it.cpQz == null || it.rcQz == null || it.htQz == null || it.rwQz == null || it.gzQz == null){

                            this.$message.warning("所有权重不能为空");
                            throw new Error('end') // 终止循环

                        }

                        if(it.xs != null && it.xs != ""){
                            let zf = Number(((it.cpScore*it.cpQz/100)+(it.rcScore*it.rcQz/100)+(it.htScore*it.htQz/100)+(it.rwScore*it.rwQz/100)+(it.gzScore*it.gzQz/100))*it.xs);
                            it.zhdf = this.numFilter(zf);
                            it.zzdf = this.numFilter(zf);

                        }else{
                            it.zhdf = ''
                            it.zzdf = '';
                        }
                    }
                })

            },
            getArrDifSameValue(arr1,arr2){
                var result = [];
                for(var i = 0; i < arr2.length; i++){
                    var obj = arr2[i];
                    var id = obj.id;
                    var isExist = false;
                    for(var j = 0; j < arr1.length; j++){
                        var aj = arr1[j];
                        var n = aj.id;
                        if(n == id){
                            isExist = true;
                            break;
                        }
                    }
                    if(!isExist){
                        result.push(obj);
                    }
                }
                return result;
            },
            numFilter(value) {
                const realVal = parseFloat(value).toFixed(2);
                return realVal;
            },
           /* //打开人员
            checkPerson(type) {
                if (type == "sjkh") {
                    this.usercomment = true;
                    this.$nextTick(() => {
                        this.$refs.usercomment.init("12", type, JSON.stringify(this.dataList),"","htcj");  //fakeData
                    });
                }

            },*/
            refresh(){//刷新

                this.getSysDeptDtoList();//部门集合
                this.getGwmcDtoList();//岗位集合
                this.getDataList();

            },
            //子组件列表
            getDataList() {
                this.dataListLoading = true;
                getMxListQuery(
                    Object.assign(
                    {
                        checkAllBm:this.checkAll,
                        checkAllGw:this.checkAllGw,
                        deptName:this.checkedBms,
                        gwmc:this.checkedGws,
                        ygname:this.searchForm.khdx,
                        ygbm:this.searchForm.ygbm,

                    }
                   ),
                    this.zid
                    ).then(r => {

                    this.dataList = r.data.data;

                });
                this.dataListLoading = false;

            },
            // 每页数
            sizeChangeHandle(val) {
                this.pageSize = val;
                this.pageIndex = 1;
                this.getDataList();
            },
            // 当前页
            currentChangeHandle(val) {
                this.pageIndex = val;
                this.getDataList();
            },
            saveFormData() {

                // 编辑
                this.fullscreenLoading = true;

                submitBmhzMx(this.dataList, this.zid).then(r => {
                    if (r.data.code == 0) {

                        this.fullscreenLoading = false;
                        //this.closeDialog();
                        this.fnBackOldPage("返回原页面");
                        this.$message.success("保存成功");
                    }
                });

            },
            delFormData(){
                if (this.multipleSelection.length == 0) {
                    this.$message.warning("请先选择要删除的数据");
                } else {
                    //删除操作
                    let ids = this.multipleSelection.map(it => {
                        return it.id;
                    });

                    this.$confirm("确定要删除勾选的数据?", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning"
                    })
                        .then(_ => {
                            deleteScore(ids.toString())
                                .then(r => {
                                    if (r.data.code == 0) {
                                        this.$message.success("删除成功");
                                        this.getDataList();
                                    } else {
                                        this.$message.error(r.data.msg);
                                        this.getDataList();
                                    }
                                })
                                .catch(err => {});
                        })
                        .catch(_ => {
                            this.getDataList();
                        });
                }
            },
            //返回原来的页面
            fnBackOldPage(type) {
                this.$emit("fnBackOldPage", type);
            },
            closeDialog() {
                this.visible = false;
                this.$emit("closeDialog");
            }
        }

    };
</script>
<style lang="scss" scoped>
  .fla {
    float: left;
    margin-right: 15px !important;
    margin-left: 5px;
  }
</style>
